*******************************************************************************
* Load data
*******************************************************************************

use dataset_gallup, clear

*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

preserve

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
local ///
legend1 legend(order(1 "{fontface monospace:{bf:Confidence in Police}}" ///
	2 "{fontface monospace:{bf:Synthetic Control}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)))
local ///
legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)))
	
}

*******************************************************************************
* Make lags
*******************************************************************************

local lags
forvalues i = 1993(1)2013 {
	egen conf`i'm = mean(conf_) if year == `i', by(target)
	egen conf`i' = mean(conf`i'm), by(target)
	drop conf`i'm
	local lags `lags' conf`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "police"

*******************************************************************************
* Synth
*******************************************************************************

synth conf_ `lags', trunit(`r(mean)') trperiod(2014) ///
	keep(tmp`subgroup', replace)

*******************************************************************************
* Save the unweighted data for now
*******************************************************************************

save tmp, replace

*******************************************************************************
* Read in the weights for the "all" subgroup
*******************************************************************************

use tmpAll, clear
rename _W_ wtall
keep _Co_ wtall

*******************************************************************************
* Merge with weights for this subgroup
*******************************************************************************

drop if mi(_Co_)
merge 1:m _Co using tmp`subgroup'
drop if mi(_Co)
keep if _m == 3
rename _W_ wtsub

*******************************************************************************
* Merge back with unweighted data
*******************************************************************************

keep _Co_ wtall wtsub
decode _Co_, gen(target)
merge 1:m target using tmp
erase tmp.dta

*******************************************************************************
* Make the SC's
*******************************************************************************

keep target year wtall wtsub conf_
reshape i year
reshape j target, string
reshape xi 
reshape xij conf_ wtall wtsub
reshape wide

local targets banks big_business church congress med_sys ///
	military newspapers org_labor police presidency schools sup_ct tv_news

local wtds
foreach l in `targets' {
	gen wtd`l'alt = wtall`l'*conf_`l'
	local wtds `wtds' wtd`l'alt
}
egen _Y_synthetic_alt = rowtotal(`wtds')

local wtds
foreach l in `targets' {
	gen wtd`l' = wtsub`l'*conf_`l'
	local wtds `wtds' wtd`l'
}
egen _Y_synthetic = rowtotal(`wtds')

rename conf_police _Y_treated 
rename year _time

*******************************************************************************
* Graph
*******************************************************************************

#delimit;

gr tw
	(line _Y_treated _time, col(black) lwid(thick))
	(line _Y_synthetic _time, col(gray) lwid(thick))
	(line _Y_synthetic_alt _time, col(red) lpat(solid))
	,
		name(g1, replace)
		xline(2014, lpat(dash))
		ylab(0.2(0.2)0.8, angle(horiz))
		plotregion(style(none))
		ytitle("")
		xtitle("")
		`legend1'	
		xlab(1990(8)2022)		
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

gen d = _Y_treated - _Y_synthetic
gen d_alt = _Y_treated - _Y_synthetic_alt

#delimit;

gr tw
	(line d _time, col(black) lwid(thick))
	(line d_alt _time, col(red) lpat(solid))
	(sc d _time, msym(none))
	,
		name(g2, replace)
		yline(0, lpat(shortdash))
		xline(2014, lpat(dash))
		plotregion(style(none))
		ylab(-.3(0.2)0.5, angle(horiz))
		ytitle("")
		xtitle("")
		xlab(1990(8)2022)		
		`legend2'
		;

#delimit cr

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* Close subgroup loop
*******************************************************************************

restore

}

*******************************************************************************
* Combine graphs
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Clean up
*******************************************************************************

erase tmpAll.dta
erase tmpDemocrat.dta
erase tmpRepublican.dta

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_sameweights_gallup.pdf", replace

*******************************************************************************
* End
*******************************************************************************
